var constant = require('../common/constant');
registController.$inject=['$scope','$location','registService'];

function  registController($scope,$location,registService) {

    $scope.submit = function () {
        $scope.msgNull = false;
        $scope.pwdError = false;
        $scope.accountError = false;
        $scope.dbError = false;
        var account = $scope.account;
        var name = $scope.name;
        var pwd = $scope.pwd;
        var pwd2 = $scope.pwd2;
        var address = $scope.address;
        var imgCard = $scope.imgCard;
        var imgHead = $scope.imgHead;
        if(isUndefined(account)||isUndefined(name)||isUndefined(pwd)||isUndefined(pwd2)||isUndefined(address)||isUndefined(imgCard)||isUndefined(imgHead)){
            $scope.msgNull = true;
        }else if(pwd!=pwd2){
            $scope.pwdError = true;
        }else{
            var merchant = {};
            merchant.account = account;
            merchant.name = name;
            merchant.pwd = pwd;
            merchant.address = address;
            merchant.imgCard = imgCard;
            merchant.imgHead = imgHead;
            registService.regist(merchant,function(response){
                if(!response.data.result){
                    if(response.data.code==constant.AccountExit){
                        $scope.accountError = true;
                    }else{
                        $scope.dbError = true;
                    }
                }else{
                    console.log("regist sucess");
                    $location.path('/');
                }
            });
        }

    }
    
    function isUndefined(obj) {
        if(!obj){
            return true;
        }
        return false;
    }



}

module.exports = registController;